#include "fsxwebservicehelper.h"
using namespace FsxWebService;

//
// File name and path.
//
QString FsxWebServiceHelper::getConfigFilename() {
    QString f = QApplication::applicationDirPath() + "/config/WebService.ini";
    if (!QFile::exists(f)) return "";
    return f;
}

//
// Content of config file.
//
QString FsxWebServiceHelper::getConfigFile() {
    QString fileName = FsxWebServiceHelper::getConfigFilename();
    if (fileName.isEmpty()) return "";
    QFile file(fileName);
    file.open(QIODevice::ReadOnly);
    QString content;
    while( !file.atEnd() ) {
        content += file.readLine();
    }
    return content;
}

//
// Update the settings.
//
bool FsxWebServiceHelper::updateSettings(const int port) {
    QString f = FsxWebServiceHelper::getConfigFilename();
    if (!QFile::exists(f)) return false;

    QSettings settings(f, QSettings::IniFormat);
    settings.beginGroup("listener");
    settings.setValue("port", port);
    settings.endGroup();
    settings.sync();
    return true;
}

//
// Get the few settings which are editable.
//
bool FsxWebServiceHelper::getEditableSettings(int &port) {
    QString f = QDir::currentPath() + "/config/WebService.ini";
    if (!QFile::exists(f)) return false;

    bool success;
    QSettings settings(f, QSettings::IniFormat);
    settings.beginGroup("listener");
    port = settings.value("port").toInt(&success);
    settings.endGroup();
    return success;
}
